﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: WaldTest.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using BOL.Maths.Distributions;

namespace BOL.Maths.Statistics.HypothesisTest
{
    /// <summary>
    /// Represents the Wald test that compares the maximum likelihood estimate of the parameter of interest θ with the proposed value θ0, with the assumption that the difference between the two will be approximately normal (or chi-square).
    /// <see cref="http://en.wikipedia.org/wiki/Wald_test"/>
    /// </summary>
    public sealed class WaldTest : TestBase
    {
        #region Private variables

        private readonly double _theta;
        private readonly double _thetaVar;
        private readonly double _thetaSE;

        #endregion

        #region Public properties

        public override double TestStatistic { get { return Distribution is ChiSquareDistribution ? Math.Pow(_theta - NullValue, 2) / _thetaVar : (_theta - NullValue) / _thetaSE; } }

        #endregion

        #region Constructors

        public WaldTest(double alpha, double theta0, double theta, double thetaVar, int df)
            : base(TestType.Upper, alpha, theta0)
        {
            Alpha = alpha;
            _theta = theta;
            _thetaVar = thetaVar;
            Distribution = new ChiSquareDistribution(df);
        }

        public WaldTest(double alpha, double theta0, double theta, double thetaSE)
            : base(TestType.Upper, alpha, theta0)
        {
            Alpha = alpha;
            _theta = theta;
            _thetaSE = thetaSE;
            Distribution = new GaussianDistribution();
        }

        #endregion
        
        #region Protected methods

        protected override double PointEstimate()
        {
            throw new Exception("Point estimate is undefined for Wald test.");
        }

        protected override double PointEstimateStandardError()
        {
            throw new Exception("Point estimate standard error is Wald test.");
        }

        #endregion

        #region Object overriden

        public override string ToString()
        {
            return String.Format("Wald Test ({0} = {1:G4}, p-value = {2:G3}): {3}", Distribution is ChiSquareDistribution ? "X^2" : "Z", TestStatistic, PValue, (IsSignificant) ? "Reject H0" : "Accept H0");
        }

        #endregion
    }
}
